#include "win3_sell.h"

int set = 0;
commodity_list_p head; // 商品链表头
pLinkList cart_head;   // 购物车链表头
lv_obj_t *img;

lv_obj_t *win3_commodity; // 商品窗口

//static lv_style_t style;

lv_obj_t *win3;
lv_obj_t **commodity_btn;
lv_obj_t *shopping_cart; // 购物车
int cart_y = 5;

lv_obj_t *Total;         // 总价区
int commodity_total = 0; // 商品总价
lv_obj_t *Total_label;   // 总价区标签

lv_obj_t *btn1;
lv_obj_t *btn2;
lv_obj_t *btn3;
lv_obj_t *btn4;

pthread_t time_pd; // 定时器线程
int time_flag = 0; // 定时器标志位
int style_flag=0;
lv_timer_t *timer1;

int set_time = 0;

static void my_timer1(lv_timer_t *timer)
{
    if (set_time == 20)
    {
        lv_obj_del(win3);
        ListDestory(&cart_head);
        Lis_commty_tDestory(&head);
        commodity_total = 0;
        cart_y = 5;
        set_time = 0;
        style_flag=1;
        lv_timer_del(timer1);
        win5_advertising();
    }
    set_time++;
    
}

void list_event_cb(lv_event_t *e) // 商品事件
{
    set_time = 0;
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *event_btn = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED)
    {
        if (event_btn == btn1)
        {
            free(commodity_btn); // 释放内存
            lv_obj_del(win3_commodity);
            show_commodity_type(head, 1);
        }
        if (event_btn == btn2)
        {
            free(commodity_btn); // 释放内存
            lv_obj_del(win3_commodity);
            show_commodity_type(head, 2);
        }
        if (event_btn == btn3)
        {
            free(commodity_btn); // 释放内存
            lv_obj_del(win3_commodity);
            show_commodity_type(head, 3);
        }
    }
}

void Eoeer_event_cb(lv_event_t *e) // 返回事件
{
    set_time = 0;
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *event_btn = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED)
    {
    }
}

void purchase_event_cb(lv_event_t *e) // 购买事件
{
    set_time = 0;
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *event_btn = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED)
    {
        if (cart_head->next == NULL)
        {
            // 创建一个消息框
        }
        else
        {
            if (RDWR_order(cart_head) == 0)
            {
                style_flag=1;
                cart_y = 5;
                lv_timer_del(timer1); // 删除定时器
                lv_obj_del(win3);
                printf("订单生成成功\n");
                WIN4_payment(); // 跳转到支付界面
            }
            else
            {
                printf("订单生成失败失败\n");
            }
        }
    }
}

static void cart_delete_cb(lv_event_t *e) // 购物车删除事件
{
    set_time = 0;
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *event_btn = lv_event_get_target(e);
    char *data = lv_event_get_user_data(e); // 获取用户数据
    if (code == LV_EVENT_CLICKED)
    {
        printf("====1====%s\n", data);
        char price[128] = {0};
        cart_delete(data, price);
        printf("price = %s\n", price);
        ListDelete(cart_head, data);
        lv_obj_del(event_btn);
        commodity_total = commodity_total - atoi(price);
        lv_label_set_text_fmt(Total_label, "%d￥", commodity_total);
    }
}

// 商品加入购物车按钮
static void commodity_btn_cb(lv_event_t *e) // 按钮事件函数
{
    set_time = 0;
    lv_event_code_t code = lv_event_get_code(e); // 同一个按钮发出不同不同事件
    lv_obj_t *event_btn = lv_event_get_target(e);
    commodity_list_p p = head;
    int i = 0;
    while (commodity_btn[i] != NULL)
    {
        if (event_btn == commodity_btn[i])
        {
            while (p != NULL)
            {
                if (p->Location == i)
                {
                    char picture_path[255] = {0};
                    lv_obj_t *shopping_cart_sre = lv_obj_create(shopping_cart);
                    lv_obj_set_size(shopping_cart_sre, 100, 100);
                    lv_obj_set_style_pad_all(shopping_cart_sre, 0, 0);
                    lv_obj_clear_flag(shopping_cart_sre, LV_OBJ_FLAG_SCROLLABLE);
                    lv_obj_add_event_cb(shopping_cart_sre, cart_delete_cb, LV_EVENT_CLICKED, p->commodity->commodity_id);
                    lv_obj_align(shopping_cart_sre, LV_ALIGN_TOP_MID, -3, cart_y);
                    if (LocateElem(cart_head, p->commodity->commodity_id) == 1)
                    {
                        ModifyElem(cart_head, p->commodity->commodity_id);
                    }
                    else
                    {
                        InsertLinst(cart_head, 1, p->commodity->commodity_id);
                    }
                    sprintf(picture_path, "A:./Data/commodity_picture/%s.png", p->commodity->commodity_id);
                    lv_obj_t *img;
                    img = lv_image_create(shopping_cart_sre);
                    lv_image_set_src(img, picture_path);
                    lv_img_set_zoom(img, 180);
                    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);

                    commodity_total = commodity_total + atoi(p->commodity->commodity_price);
                    lv_label_set_text_fmt(Total_label, "%d￥", commodity_total);
                    cart_y += 105;
                }
                p = p->next;
            }
        }
        i++;
    }
}

static void anim_x_cb(void *var, int32_t v)
{
    lv_obj_set_x(var, v);
}

static void sw_event_cb(lv_event_t *e)
{
    lv_obj_t *sw = lv_event_get_target(e);
    lv_obj_t *label = lv_event_get_user_data(e);
    set_time = 0;
    if (set == 0)
    {
        lv_anim_t a;
        lv_anim_init(&a);
        lv_anim_set_var(&a, label);
        lv_anim_set_values(&a, -150, 0);
        lv_anim_set_duration(&a, 500);
        lv_anim_set_exec_cb(&a, anim_x_cb);
        lv_anim_set_path_cb(&a, lv_anim_path_overshoot);
        lv_anim_start(&a);
        LV_IMG_DECLARE(appear);
        lv_img_set_src(img, &appear);
        set = 1;
    }
    else
    {
        lv_anim_t a;
        lv_anim_init(&a);
        lv_anim_set_var(&a, label);
        lv_anim_set_values(&a, 0, -150);
        lv_anim_set_duration(&a, 500);
        lv_anim_set_exec_cb(&a, anim_x_cb);
        lv_anim_set_path_cb(&a, lv_anim_path_ease_in);
        lv_anim_start(&a);
        set = 0;
        LV_IMG_DECLARE(hide);
        free(commodity_btn); // 释放内存
        lv_img_set_src(img, &hide);
        lv_obj_del(win3_commodity);
        show_commodity(head); // 显示商品
        printf("set=%d\n", set);
    }
}

// 购物车主界面
void win3_sell(void)
{

    InitList(&cart_head);
    COMMODITY_LIST_Init(&head);
    COMMODITY_LIST_show_list(head);
    COMMODITY_LIST_Update(head); // 更新商品链表

    // lv_font_t *font = lv_freetype_font_create("/root/simkai.ttf", // 字体文件路径
    //                                           LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
    //                                           24,
    //                                           LV_FREETYPE_FONT_STYLE_NORMAL);

    // if (!font)
    // {
    //     printf("freetype font create failed.\n");
    //     return;
    // }
    // if (style_flag == 0)
    // {
    //     /*Create style with the new font*/
    //     lv_style_init(&style);
    //     lv_style_set_text_font(&style, font);
    //     lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
    // }

    win3 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(win3, 800, 480);
    lv_obj_set_style_pad_all(win3, 0, 0);
    lv_obj_clear_flag(win3, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_align(win3, LV_ALIGN_CENTER);
    printf("====1=====\n");
    show_commodity(head); // 显示商品
    printf("====1=====\n");

    // lv_obj_t *list = lv_list_create(win3);
    // lv_obj_add_style(list, &style, 0);
    // lv_obj_set_size(list, 120, 200);
    // lv_obj_align(list, LV_ALIGN_LEFT_MID, -120, 0);
    // // 2.添加列表文本
    // lv_list_add_text(list, "分类");
    // btn1 = lv_list_add_btn(list, NULL, "饮料");
    // btn2 = lv_list_add_btn(list, NULL, "零食");
    // btn3 = lv_list_add_btn(list, NULL, "水果");
    // lv_obj_add_event_cb(btn1, list_event_cb, LV_EVENT_CLICKED, list);
    // lv_obj_add_event_cb(btn2, list_event_cb, LV_EVENT_CLICKED, list);
    // lv_obj_add_event_cb(btn3, list_event_cb, LV_EVENT_CLICKED, list);

    // lv_obj_t *list_btn = lv_btn_create(win3);
    // lv_obj_set_size(list_btn, 40, 40);
    // lv_obj_set_style_bg_color(list_btn, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    // lv_obj_add_event_cb(list_btn, sw_event_cb, LV_EVENT_CLICKED, list);
    // lv_obj_align(list_btn, LV_ALIGN_LEFT_MID, 0, -110);
    // img = lv_img_create(list_btn);
    // LV_IMG_DECLARE(hide);
    // lv_img_set_src(img, &hide);
    // lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *purchase_btn = lv_btn_create(win3);
    lv_obj_set_size(purchase_btn, 130, 50);
    lv_obj_set_style_bg_color(purchase_btn, lv_color_hex(0x0FFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_align(purchase_btn, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
    lv_obj_add_event_cb(purchase_btn, purchase_event_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *purchase_label = lv_label_create(purchase_btn);
    lv_obj_add_style(purchase_label, &style_win3, 0);
    lv_label_set_text(purchase_label, "确定");
    lv_obj_center(purchase_label);
    // lv_obj_add_event_cb(purchase_btn, purchase_event_cb, LV_EVENT_CLICKED, NULL);

    shopping_cart = lv_obj_create(win3);
    lv_obj_set_style_pad_all(shopping_cart, 0, 0);
    lv_obj_set_scroll_dir(shopping_cart, LV_DIR_VER);
    lv_obj_set_size(shopping_cart, 130, 300);
    lv_obj_set_scroll_dir(shopping_cart, LV_DIR_VER);
    lv_obj_set_style_bg_color(shopping_cart, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_align_to(shopping_cart, purchase_btn, LV_ALIGN_OUT_TOP_MID, 0, 0);

    Total = lv_obj_create(win3);
    lv_obj_set_style_pad_all(Total, 0, 0);
    lv_obj_clear_flag(Total, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(Total, 130, 50);
    lv_obj_set_style_bg_color(Total, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_align_to(Total, shopping_cart, LV_ALIGN_OUT_TOP_MID, 0, 0);
    Total_label = lv_label_create(Total);
    lv_obj_add_style(Total_label, &style_win3, 0);
    lv_obj_align(Total_label, LV_ALIGN_CENTER, 0, 0);
    printf("+====3====\n");
    lv_label_set_text_fmt(Total_label, "%d￥", commodity_total);

    lv_obj_t *Eoeer = lv_obj_create(win3);
    lv_obj_set_size(Eoeer, 130, 80);
    lv_obj_set_style_bg_color(Eoeer, lv_color_hex(0x00FFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_align_to(Eoeer, Total, LV_ALIGN_OUT_TOP_MID, 0, 0);
    lv_obj_add_event_cb(Eoeer, Eoeer_event_cb, LV_EVENT_CLICKED, NULL);

    timer1 = lv_timer_create(my_timer1, 1000, NULL);
    lv_mem_monitor_t mon;
    lv_mem_monitor(&mon);
    printf("Free memory: %zu\n", mon.free_size);
}

// 商品列表显示
void show_commodity(commodity_list_p ptr)
{
    win3_commodity = lv_obj_create(win3); // 商品列表区
    lv_obj_set_size(win3_commodity, 670, 480);
    lv_obj_set_scroll_dir(win3_commodity, LV_DIR_VER);
    lv_obj_set_style_pad_all(win3_commodity, 0, 0); // 设置内边距
    lv_obj_set_style_bg_color(win3_commodity, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_align(win3_commodity, LV_ALIGN_BOTTOM_LEFT);
    commodity_btn = malloc(sizeof(lv_obj_t *) * WIN3_COMMODITY_NUM);

    commodity_list_t *p = ptr->next;
    int i = 0;
    int x = 5;
    int y = 5;
    int set_x = 0;

    while (p != NULL)
    {
        char picture_path[255] = {0};
        p->Location = i;                                     // 记录商品在链表中的位置
        lv_obj_t *commodity = lv_obj_create(win3_commodity); // 这个全局变量可能没用
        lv_obj_set_size(commodity, 150, 200);
        lv_obj_set_style_pad_all(commodity, 0, 0);
        lv_obj_clear_flag(commodity, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_style_bg_color(commodity, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_align(commodity, LV_ALIGN_TOP_LEFT, x, y);

        sprintf(picture_path, "A:./Data/commodity_picture/%s.png", p->commodity->commodity_id);
        lv_obj_t *img;
        img = lv_image_create(commodity);
        lv_image_set_src(img, picture_path);
        lv_obj_align(img, LV_ALIGN_TOP_MID, 0, 0);

        commodity_btn[i] = lv_btn_create(commodity);
        lv_obj_set_size(commodity_btn[i], 80, 40);
        lv_obj_align(commodity_btn[i], LV_ALIGN_BOTTOM_RIGHT, 0, 0);
        lv_obj_t *btn_label = lv_label_create(commodity_btn[i]);
        lv_obj_add_style(btn_label, &style_win3, 0);
        lv_label_set_text(btn_label, "购买");
        lv_obj_add_event_cb(commodity_btn[i], commodity_btn_cb, LV_EVENT_CLICKED, NULL);

        lv_obj_t *price = lv_obj_create(commodity);
        lv_obj_clear_flag(price, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_style_pad_all(price, 0, 0);
        lv_obj_set_size(price, 70, 40);
        lv_obj_align(price, LV_ALIGN_BOTTOM_LEFT, 0, 0);
        lv_obj_t *price_label = lv_label_create(price);
        lv_obj_add_style(price_label, &style_win3, 0);
        lv_label_set_text_fmt(price_label, "%s￥", p->commodity->commodity_price);
        lv_obj_align(price_label, LV_ALIGN_CENTER, 0, 0);
        i++;
        if (set_x < 3)
        {
            set_x++;
            x = x + 160;
        }
        else
        {
            set_x = 0;
            x = 10;
            y = y + 210;
        }
        p = p->next;
    }
    lv_obj_t *list = lv_list_create(win3_commodity);
    lv_obj_add_style(list, &style_win3, 0);
    lv_obj_set_size(list, 120, 200);
    lv_obj_align(list, LV_ALIGN_LEFT_MID, -120, 0);
    // 2.添加列表文本
    lv_list_add_text(list, "分类");
    btn1 = lv_list_add_btn(list, NULL, "饮料");
    btn2 = lv_list_add_btn(list, NULL, "零食");
    btn3 = lv_list_add_btn(list, NULL, "泡面");
    btn4 = lv_list_add_btn(list, NULL, "其他");
    lv_obj_add_event_cb(btn1, list_event_cb, LV_EVENT_CLICKED, list);
    lv_obj_add_event_cb(btn2, list_event_cb, LV_EVENT_CLICKED, list);
    lv_obj_add_event_cb(btn3, list_event_cb, LV_EVENT_CLICKED, list);

    lv_obj_t *list_btn = lv_btn_create(win3_commodity);
    lv_obj_set_size(list_btn, 50, 50);
    lv_obj_set_style_bg_color(list_btn, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_add_event_cb(list_btn, sw_event_cb, LV_EVENT_CLICKED, list);
    lv_obj_align(list_btn, LV_ALIGN_LEFT_MID, 0, -110);
    img = lv_img_create(list_btn);
    LV_IMG_DECLARE(hide);
    lv_img_set_src(img, &hide);
    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
}

void cart_delete(char *id, char *price_t)
{
    commodity_list_t *p = head->next;
    while (p != NULL)
    {
        if (strcmp(p->commodity->commodity_id, id) == 0)
        {

            strcpy(price_t, p->commodity->commodity_price);
            break;
        }
        p = p->next;
    }
}

void show_commodity_type(commodity_list_p ptr, int type_t)
{

    win3_commodity = lv_obj_create(win3); // 商品列表区
    lv_obj_set_size(win3_commodity, 670, 480);
    lv_obj_set_scroll_dir(win3_commodity, LV_DIR_VER);
    lv_obj_set_style_pad_all(win3_commodity, 0, 0);
    lv_obj_set_style_bg_color(win3_commodity, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_align(win3_commodity, LV_ALIGN_BOTTOM_LEFT);
    commodity_btn = malloc(sizeof(lv_obj_t *) * WIN3_COMMODITY_NUM);
    commodity_list_t *p = ptr->next;
    int i = 0;
    int x = 5;
    int y = 5;
    int set_x = 0;

    while (p != NULL)
    {
        if (atoi(p->commodity->commodity_type) == type_t)
        {
            char picture_path[255] = {0};
            p->Location = i;                                     // 记录商品在链表中的位置
            lv_obj_t *commodity = lv_obj_create(win3_commodity); // 这个全局变量可能没用
            lv_obj_set_size(commodity, 150, 200);
            lv_obj_set_style_pad_all(commodity, 0, 0);
            lv_obj_clear_flag(commodity, LV_OBJ_FLAG_SCROLLABLE);
            lv_obj_set_style_bg_color(commodity, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_align(commodity, LV_ALIGN_TOP_LEFT, x, y);

            sprintf(picture_path, "A:./Data/commodity_picture/%s.png", p->commodity->commodity_id);
            lv_obj_t *img;
            img = lv_image_create(commodity);
            lv_image_set_src(img, picture_path);
            lv_obj_align(img, LV_ALIGN_TOP_MID, 0, 0);

            commodity_btn[i] = lv_btn_create(commodity);
            lv_obj_set_size(commodity_btn[i], 80, 40);
            lv_obj_align(commodity_btn[i], LV_ALIGN_BOTTOM_RIGHT, 0, 0);
            lv_obj_t *btn_label = lv_label_create(commodity_btn[i]);
            lv_obj_add_style(btn_label, &style_win3, 0);
            lv_label_set_text(btn_label, "购买");
            lv_obj_add_event_cb(commodity_btn[i], commodity_btn_cb, LV_EVENT_CLICKED, NULL);

            lv_obj_t *price = lv_obj_create(commodity);
            lv_obj_clear_flag(price, LV_OBJ_FLAG_SCROLLABLE);
            lv_obj_set_style_pad_all(price, 0, 0);
            lv_obj_set_size(price, 70, 40);
            lv_obj_align(price, LV_ALIGN_BOTTOM_LEFT, 0, 0);
            lv_obj_t *price_label = lv_label_create(price);
            lv_obj_add_style(price_label, &style_win3, 0);
            lv_label_set_text_fmt(price_label, "%s￥", p->commodity->commodity_price);
            lv_obj_align(price_label, LV_ALIGN_CENTER, 0, 0);
            i++;
            if (set_x < 3)
            {
                set_x++;
                x = x + 160;
            }
            else
            {
                set_x = 0;
                x = 10;
                y = y + 210;
            }
        }
        p = p->next;
    }
    lv_obj_t *list = lv_list_create(win3_commodity);
    lv_obj_add_style(list, &style_win3, 0);
    lv_obj_set_size(list, 120, 200);
    lv_obj_align(list, LV_ALIGN_LEFT_MID, 0, 0);
    // 2.添加列表文本
    lv_list_add_text(list, "分类");
    btn1 = lv_list_add_btn(list, NULL, "饮料");
    btn2 = lv_list_add_btn(list, NULL, "零食");
    btn3 = lv_list_add_btn(list, NULL, "泡面");
    btn4 = lv_list_add_btn(list, NULL, "其他");
    lv_obj_add_event_cb(btn1, list_event_cb, LV_EVENT_CLICKED, list);
    lv_obj_add_event_cb(btn2, list_event_cb, LV_EVENT_CLICKED, list);
    lv_obj_add_event_cb(btn3, list_event_cb, LV_EVENT_CLICKED, list);

    lv_obj_t *list_btn = lv_btn_create(win3_commodity);
    lv_obj_set_size(list_btn, 50, 50);
    lv_obj_set_style_bg_color(list_btn, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_add_event_cb(list_btn, sw_event_cb, LV_EVENT_CLICKED, list);
    lv_obj_align(list_btn, LV_ALIGN_LEFT_MID, 0, -110);
    img = lv_img_create(list_btn);
    LV_IMG_DECLARE(hide);
    lv_img_set_src(img, &hide);
    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
}

// void show_cart(void)
// {
//     lv_obj_del(shopping_cart);
//     lv_obj_set_style_pad_all(shopping_cart, 0, 0);
//     lv_obj_set_scroll_dir(shopping_cart, LV_DIR_VER);
//     lv_obj_set_size(shopping_cart, 130, 300);
//     lv_obj_set_scroll_dir(shopping_cart, LV_DIR_VER);
//     lv_obj_set_style_bg_color(shopping_cart, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
//     lv_obj_align_to(shopping_cart, purchase_btn, LV_ALIGN_OUT_TOP_MID, 0, 0);
//     pLinkList p=cart_head->next;
//     while (p != NULL)
//     {
//         lv_obj_t *price_label = lv_label_create(shopping_cart);
//     }

// }g
